package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.BooleanUtil;
import com.sc.nft.dao.EcologyHatchExtraRewardsConfigDao;
import com.sc.nft.dao.EcologyHatchExtraRewardsMethodConfDao;
import com.sc.nft.dao.EcologyPointsDao;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.EcologyHatchConfDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsConfigSaveDTO;
import com.sc.nft.entity.dto.EcologyHatchExtraRewardsMethodConfDTO;
import com.sc.nft.entity.req.EcologyHatchUsePropsExchangeRequest;
import com.sc.nft.enums.EcologyRewardsType;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.EcologyHatchExtraRewardsConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 朱梦晨
 * @Description:
 * @date 2024-09-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyHatchExtraRewardsConfigServiceImpl  implements EcologyHatchExtraRewardsConfigService {


    private final EcologyHatchExtraRewardsConfigDao ecologyHatchExtraRewardsConfigDao;

    private final EquityPropsDao equityPropsDao;

    private final EcologyHatchConfDao ecologyHatchConfDao;

    private final EcologyPointsDao ecologyPointsDao;

    /**
     * 编辑奖励配置
     * @param ecologyHatchId
     * @param extraRewardsId
     * @param ecologyHatchExtraRewardsConfigSaveDTOS
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editEcologyHatchExtraRewardsConfig(Long ecologyHatchId, Long extraRewardsId,Long methodId,
                                                  List<EcologyHatchExtraRewardsConfigSaveDTO> ecologyHatchExtraRewardsConfigSaveDTOS){
        // 生态额外奖励配置关联表
        List<EcologyHatchExtraRewardsConfig> ecologyHatchExtraRewardsConfigList = findEcologyHatchExtraRewardsConfigListByExtraRewardsId(extraRewardsId,methodId);
        List<Long> idsInDb = ecologyHatchExtraRewardsConfigList.stream().map(EcologyHatchExtraRewardsConfig::getId).collect(Collectors.toList());

        for (EcologyHatchExtraRewardsConfigSaveDTO dto : ecologyHatchExtraRewardsConfigSaveDTOS) {
            if (dto.getRewardsType().equals(EcologyRewardsType.EQUITY_PROPS)){
                //道具
                EquityProps equityProps = equityPropsDao.getById(dto.getLinkId());
                Assert.notNull(equityProps, () -> new GlobalRunTimeException("道具卡不存在"));
                dto.setLinkName(equityProps.getFirstTitle());
                dto.setLinkImage(equityProps.getCreationContent());
            }else if (dto.getRewardsType().equals(EcologyRewardsType.ECOLOGY_MARK)){
                //积分
                EcologyPoints ecologyPoints = ecologyPointsDao.getById(dto.getLinkId());
                Assert.notNull(ecologyPoints, () -> new GlobalRunTimeException("积分不存在"));
                dto.setLinkName(ecologyPoints.getPointsName());
                dto.setLinkImage(ecologyPoints.getPointsImage());
            }
            Long id = dto.getId();
            if (Objects.isNull(id)){
                EcologyHatchExtraRewardsConfig ecologyHatchExtraRewardsConfig = BeanUtil.copyProperties(dto, EcologyHatchExtraRewardsConfig.class);
                ecologyHatchExtraRewardsConfig.setEcologyHatchId(ecologyHatchId);
                ecologyHatchExtraRewardsConfig.setExtraRewardsId(extraRewardsId);
                ecologyHatchExtraRewardsConfig.setMethodId(methodId);
                ecologyHatchExtraRewardsConfig.insert();
            }else {
                idsInDb.remove(id);
                // 修改
                Assert.isTrue(ecologyHatchExtraRewardsConfigDao.updateEcologyHatchExtraRewardsConfig(id,dto.getDeliveryCycle(),dto.getLinkId(),dto.getLinkCount(),
                        dto.getLinkImage(),dto.getLinkName(),dto.getRewardsType()), () -> new GlobalRunTimeException("奖励配置更新失败"));
            }
        }
        if (!CollectionUtils.isEmpty(idsInDb)){
            ecologyHatchExtraRewardsConfigDao.removeByIds(idsInDb);
        }
    }

    @Override
    public List<EcologyHatchExtraRewardsConfig> findEcologyHatchExtraRewardsConfigListByExtraRewardsId(Long extraRewardsId,Long methodId) {
        return ecologyHatchExtraRewardsConfigDao.findEcologyHatchExtraRewardsConfigListByExtraRewardsId(extraRewardsId,methodId);
    }


    @Override
    public void deleteEcologyHatchExtraRewardsConfigById(Long id) {
        EcologyHatchExtraRewardsConfig ecologyHatchExtraRewardsConfig = ecologyHatchExtraRewardsConfigDao.getById(id);
        Assert.notNull(ecologyHatchExtraRewardsConfig, () -> new GlobalRunTimeException("生态额外奖励配置不存在"));
        EcologyHatchConf ecologyHatchConfInDb = ecologyHatchConfDao.getById(ecologyHatchExtraRewardsConfig.getEcologyHatchId());
        Assert.isFalse(ecologyHatchConfInDb.getStatus(),() -> new GlobalRunTimeException("请先下架熔炼配置"));
        Assert.isTrue(ecologyHatchExtraRewardsConfigDao.deleteEcologyHatchExtraRewardsConfigById(id), () -> new GlobalRunTimeException("删除失败"));
    }


}
